//
//  SetUpViewController.m
//  LiquorChain
//
//  Created by apple on 2018/12/29.
//  Copyright © 2018年 王冬. All rights reserved.
//

#import "SetUpViewController.h"
#import "PublicViewCell.h"
#import "HeadImageViewCell.h"
#import "EditNameController.h"
#import "AdressListController.h"
#import "EditPhoneController.h"
#import "EditPasdController.h"
@interface SetUpViewController ()<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong) UIImagePickerController *imagePicker;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *titleArray1;
@property (nonatomic,strong) UIImageView *headImageView;
@end

@implementation SetUpViewController
static NSString *headIdentifier = @"headCell";
static NSString *pubIdentifier = @"pubCell";
-(NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray =[NSArray array];
    }
    return _titleArray;
}

-(NSArray *)titleArray1{
    if (!_titleArray1) {
        _titleArray1 =[NSArray array];
    }
    return _titleArray1;
}
-(UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"item"]];
    }
    return _headImageView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置";
    self.titleArray = @[@"头像",@"用户名",@"手机绑定"];
    self.titleArray1 = @[@"微信绑定",@"登录密码修改",@"交易密码修改",@"收货地址"];
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, kNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT- kNavBarHeight - kIPhoneXBottomHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
   // [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier: CellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"PublicViewCell" bundle:nil] forCellReuseIdentifier:pubIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"HeadImageViewCell" bundle:nil] forCellReuseIdentifier:headIdentifier];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 3;
    }
    if (section==1) {
        return 4;
    }else{
        return 1;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
    headView.backgroundColor =BACKGROUNDCOLOR;
    return headView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        if (indexPath.row ==0) {
            return 70;
        }
    }if (indexPath.section ==1) {
        return 50;
    }else{
        return 50;
    }

    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section ==2) {
        UIView *bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        bottomView.backgroundColor = BACKGROUNDCOLOR;
        UIButton *editBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        editBtn.backgroundColor =[UIColor whiteColor];
        editBtn.layer.cornerRadius = 5;
        [editBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [editBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [editBtn addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
        editBtn.titleLabel.font =[UIFont systemFontOfSize:18];
        [bottomView addSubview:editBtn];
        [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(25);
            make.left.mas_equalTo(14);
            make.right.mas_equalTo(-14);
            make.height.mas_equalTo(50);
        }];
        return bottomView;
    }else{
        return [[UIView alloc]init];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section ==2) {
        return 100;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row ==0) {
            HeadImageViewCell *cell =[tableView dequeueReusableCellWithIdentifier:headIdentifier];
            cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.nameLabel.text = @"头像";
            cell.headImageView.image = self.headImageView.image;
            return cell;
        }else{
            PublicViewCell *cell =[tableView dequeueReusableCellWithIdentifier:pubIdentifier];
            cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (indexPath.row == 1) {
                   cell.leftLabel.text = @"用户名";
                   cell.rightLabel.text = @"编辑";
               }if (indexPath.row == 2) {
                   cell.leftLabel.text =@"电话";
                   cell.rightLabel.text = @"122122142";
              }
            
            return cell;
        }
    }else{
        PublicViewCell *cell =[tableView dequeueReusableCellWithIdentifier:pubIdentifier];
        cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.section ==1) {
            cell.leftLabel.text = self.titleArray1[indexPath.row];
        }else{
            cell.leftLabel.text = @"清除缓存";
        }
        return cell;
    }

   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
        if (indexPath.row ==0) {
            /**
             *  弹出提示框
             */
            //初始化提示框
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            //按钮：从相册选择，类型：UIAlertActionStyleDefault
            [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //初始化UIImagePickerController
                UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
                //获取方式1：通过相册（呈现全部相册），UIImagePickerControllerSourceTypePhotoLibrary
                //获取方式2，通过相机，UIImagePickerControllerSourceTypeCamera
                //获取方法3，通过相册（呈现全部图片），UIImagePickerControllerSourceTypeSavedPhotosAlbum
                PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                //允许编辑，即放大裁剪
                PickerImage.allowsEditing = YES;
                //自代理
                PickerImage.delegate = self;
                //页面跳转
                [self presentViewController:PickerImage animated:YES completion:nil];
            }]];
            //按钮：拍照，类型：UIAlertActionStyleDefault
            [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
                /**
                 其实和从相册选择一样，只是获取方式不同，前面是通过相册，而现在，我们要通过相机的方式
                 */
                UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
                //获取方式:通过相机
                PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
                PickerImage.allowsEditing = YES;
                PickerImage.delegate = self;
                [self presentViewController:PickerImage animated:YES completion:nil];
            }]];
            //按钮：取消，类型：UIAlertActionStyleCancel
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
        if (indexPath.row ==1) {
            EditNameController *editVC =[[EditNameController alloc]init];
            [self.navigationController pushViewController:editVC animated:YES];
        }
        if (indexPath.row ==2) {
            EditPhoneController *editPVC =[[EditPhoneController alloc]init];
            [self.navigationController pushViewController:editPVC animated:YES];
        }
    }
    if (indexPath.section ==1) {
        if (indexPath.row ==0) {
            NSLog(@"微信绑定");
        }
        
        if (indexPath.row ==1) {
            EditPasdController *pasVC =[[EditPasdController alloc]init];
            [self.navigationController pushViewController:pasVC animated:YES];
        }
        if (indexPath.row ==2) {
            NSLog(@"交易密码的修改");
        }
        if (indexPath.row ==3) {
            AdressListController * adressVC =[[AdressListController alloc]init];
            [self.navigationController pushViewController:adressVC animated:YES];
        }
    }
}

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    self.headImageView.image = newPhoto;
    [self.tableView reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark 退出登录
-(void)editClick:(UIButton *)sender{
    NSLog(@"退出登录");
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}



@end
